package cz.data.gateway.admin.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import cz.data.gateway.model.dto.GatewayRouteForm;
import cz.data.gateway.model.dto.GatewayRouteVo;
import cz.data.gateway.model.dto.GatewayRouteQueryParam;
import cz.data.gateway.admin.mapstruct.GatewayRouteMapper;
import cz.data.gateway.admin.service.IGatewayRouteService;
import cz.data.gateway.model.entity.GatewayRoute;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Api("网关路由")
@RestController
@RequestMapping("/api/admin/gateway/routes")
public class GatewayRouteController {

    @Autowired
    IGatewayRouteService gatewayRoutService;
    @Autowired
    GatewayRouteMapper gatewayRouteMapper;

    @ApiOperation(value = "搜索网关路由", notes = "根据条件查询网关路由信息")
    @ApiImplicitParam(name = "gatewayRoutQueryForm", value = "网关路由查询参数", required = true, dataType = "GatewayRouteQueryForm")
    @GetMapping(value = "/page")
    public TableDataResponse<GatewayRouteVo> search(@Valid GatewayRouteQueryParam request) {
        Page<GatewayRoute> page = gatewayRoutService.page(request);
        List<GatewayRouteVo> collect = page.getRecords().stream().map(gatewayRouteMapper::toVO).collect(Collectors.toList());
        return new TableDataResponse<>(collect, page.getTotal());
    }

    @ApiOperation(value = "获取网关路由", notes = "根据id获取指定网关路由信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public SimpleDataResponse<GatewayRouteVo> get(@PathVariable String id) {
        GatewayRouteVo routeVo = Optional.ofNullable(gatewayRoutService.get(id))
                .map(gatewayRouteMapper::toVO).orElse(null);
        return new SimpleDataResponse<>(routeVo);
    }

    @ApiOperation(value = "新增网关路由", notes = "新增一个网关路由")
    @ApiImplicitParam(name = "gatewayRoutForm", value = "新增网关路由form表单", required = true, dataType = "GatewayRouteForm")
    @PostMapping
    public BaseResponse add(@Valid @RequestBody GatewayRouteForm gatewayRoutForm) {
        GatewayRoute gatewayRout = gatewayRouteMapper.toEntity(gatewayRoutForm);
        return BaseResponse.withBoolean(gatewayRoutService.add(gatewayRout));
    }

    @ApiOperation(value = "修改网关路由", notes = "修改指定网关路由信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "网关路由ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "gatewayRoutForm", value = "网关路由实体", required = true, dataType = "GatewayRouteForm")
    })
    @PutMapping
    public BaseResponse update(@Valid @RequestBody GatewayRouteForm gatewayRoutForm) {
        GatewayRoute gatewayRout = gatewayRouteMapper.toEntity(gatewayRoutForm);
        return BaseResponse.withBoolean(gatewayRoutService.update(gatewayRout));
    }

    @ApiOperation(value = "删除网关路由", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "string")
    @DeleteMapping(value = "/{id}")
    public BaseResponse delete(@PathVariable String id) {
        return BaseResponse.withBoolean(gatewayRoutService.delete(id));
    }

    @ApiOperation(value = "根据uri获取网关路由", notes = "根据uri获取网关路由信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "网关路由路径", required = true, dataType = "string")
    @GetMapping
    public SimpleDataResponse<GatewayRouteVo> getByUri(@RequestParam String uri) {
        Optional<GatewayRouteVo> routeVo = gatewayRoutService.query(new GatewayRouteQueryParam(uri)).stream().findFirst();
        return new SimpleDataResponse<>(routeVo.orElse(null));
    }

    @ApiOperation(value = "重载网关路由", notes = "将所以网关的路由全部重载到redis中")
    @PostMapping(value = "/overload")
    public BaseResponse overload() {
        return BaseResponse.withBoolean(gatewayRoutService.overload());
    }

    @ApiOperation(value = "网关路由状态变更")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "网关路由状态", required = true, dataType = "string")
    })
    @PutMapping("/status/{id}")
    public BaseResponse changeStatus(@PathVariable String id,
                                     @RequestParam("status") String status) {
        return BaseResponse.withBoolean(gatewayRoutService.changeStatus(id, status));
    }
}